package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * javabean形式配置消息队列
 * spring容器中的 Queue Exchange Binding 在消息队列里不存在的话，会自动创建出来
 * 一旦创建好，rabbit只要有，@Bean声明的配置发生变化，rabbit也不会修改，只能先删除 重新创建
 */
@Configuration
public class MyMQConfig {

    @Bean
    public Queue orderDelayQueue() {
        /**
         * 死信相关配置
         * x-dead-letter-exchange: order-event-exchange
         * x-dead-letter-routing-key: order.release.order
         * x-message-ttl: 60000
         */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);
        return new Queue(
                "order.delay.queue",
                true,
                false,
                false,
                arguments
        );
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue(
                "order.release.order.queue",
                true,
                false,
                false
        );
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(
                "order-event-exchange",
                true,
                false
        );
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        return new Binding(
                "order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null
        );
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        return new Binding(
                "order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null
        );
    }

    /**
     * 订单释放交换机，和库存释放队列，直接绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        return new Binding(
                "stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null
        );
    }
}
